package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {

	fmt.Print("Go runs on ")
	switch os := runtime.GOOS; os {
	case "darwi" + "n": //MARK 灵活的表达式
		fmt.Println("OS X.") //MARK 不用break
	case "linux":
		fmt.Println("Linux.")
	default:
		// freebsd, openbsd,
		// plan9, windows...
		fmt.Printf("%s.\n", os)
	}

	var i int
	switch i {
	case f(): //MARK 甚至可是函数 ,会被一一调用比对, 直到对上
	case 0, 2: //MARK 可多个
		fmt.Printf("%v\n", i)
	}

	t := time.Now()
	switch { //MARK 无基准 , 相当于if-then-else
	case t.Hour() < 12:
		fmt.Println("Good morning!")
	case t.Hour() < 17:
		fmt.Println("Good afternoon.")
	default:
		fmt.Println("Good evening.")
	}
	typeSwitchs()
}
func typeSwitchs() {

	fmt.Println("================拿出具体类型 switch=================")
	do(21)
	do("hello")
	do(true)
}
func do(i interface{}) {
	//v := i.(type)//  Usage of .(type) is outside of the type switch MARK  不行
	switch v := i.(type) { //MARK .(type) 这个语法只能switch用
	case int:
		//MARK 这里可以安全当int用了
		fmt.Printf("Twice %v is %v\n", v, v*2)
	case string:
		fmt.Printf("%q is %v bytes long\n", v, len(v))
	default:
		fmt.Printf("I don't know about type %T!\n", v)
	}
}

func f() int {
	fmt.Println("f()")
	return 2
}
